
var express = require('express');
var app = express();

var path = require('path');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');
var connectMongo = require('connect-mongo');
var server = require('http').createServer(app);
var io = require('socket.io')(server)
var MongoStore = connectMongo(session);


//var routes = require('./routes');
// var $  = require('./utils');
import $ from './utils';
import routes from './routes';
import MongoDB from './mongodb';


app.get('/', function (req, res) {
    res.send('Hello World!');
});

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use(session({
    name:$.config.sessionIdentityKey,
    secret:'chikago',
    store:new MongoStore({
        url:$.config.MONGODB_CONN_STR
    }),
    saveUninitialized:false,
    resave:false,
    cookie:{
        maxAge:60*60*1000
    }
}))

MongoDB.mongoFunc.connectMongo();

app.use('/message/api/User',routes.us);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

// error handler
app.use(function(err, req, res, next) {
    // set locals, only providing error in development
    res.locals.message = err.message;
    res.locals.error = req.app.get('env') === 'development' ? err : {};

    // render the error page
    res.status(err.status || 500);
    res.render('error');
});










server.listen(8090, function () {
    var host = server.address().address;
    var port = server.address().port;

    console.log('Example app listening at http://%s:%s', host, port);
});